package aod.android.com.bernay.http;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import aod.android.com.bernay.BernayApp;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.util.LogUtils;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class BernayApi {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static BernayApi mApi;

    private OkHttpClient mOkHttpClient;


    public static BernayApi http() {
        if (mApi == null) {
            mApi = new BernayApi();
        }
        return mApi;
    }

    public BernayApi() {
        mOkHttpClient = new OkHttpClient();
    }


    /**
     * 登录接口
     *
     * @param account  手机号
     * @param password 密码
     * @param callback 回调
     */
    public void login(String account, String password, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("LogName", account);
        j.addProperty("LogPwd", password);
        post(HttpUrl.LOGIN, j.toString(), callback);
    }

    /**
     * 注册接口
     *
     * @param account    手机号
     * @param code       验证码
     * @param password   密码
     * @param repassword 确认密码
     * @param callback   回调
     */
    public void register(String account, String code, String password, String repassword, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("LogName", account);
        j.addProperty("CheckCode", code);
        j.addProperty("LogPwd", password);
        j.addProperty("RePass", repassword);
        post(HttpUrl.REGISTER, j.toString(), callback);
    }

    /**
     * 找回密码接口
     *
     * @param account    手机号
     * @param code       验证码
     * @param password   密码
     * @param repassword 确认密码
     * @param callback   回调
     */
    public void resetpass(String account, String code, String password, String repassword, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("LogName", account);
        j.addProperty("CheckCode", code);
        j.addProperty("LogPwd", password);
        j.addProperty("RePass", repassword);
        post(HttpUrl.RESETPASS, j.toString(), callback);
    }

    /**
     * 找回密码接口
     *
     * @param UserId  用户ID
     * @param Token   用户token
     * @param Title   标题
     * @param Content 内容
     * @param Source  "1.jpg,2.jpg"
     * @param Type    //1是图片 2是视频
     */
    public void uploadlive(String UserId, String Token, String Title, String Content, String Source, String Type, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("UserId", UserId);
        j.addProperty("Token", Token);
        j.addProperty("Title", Title);
        j.addProperty("Content", Content);
        j.addProperty("Source", Source);
        j.addProperty("Type", Type);
        post(HttpUrl.UPDATELIVE, j.toString(), callback);
    }

    /**
     * 验证码接口
     *
     * @param account  手机号
     * @param callback 回调
     */
    public void getPhoneCode(String account, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("MobileNo", account);
        post(HttpUrl.GET_PHONE_CODE, j.toString(), callback);
    }

    /**
     * 获取直播列表
     *
     * @param uid
     * @param token
     * @param page
     * @param size
     * @param callback
     */
    public void getVideoList(String uid, String token, String page, String size, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("PageIndex", page);
        j.addProperty("PageSize", size);
        post(HttpUrl.VIDEOLIST, j.toString(), callback);
    }

    /**
     * 获取直播详情
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void getVideoInfo(String uid, String token, String id, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("id", id);
        post(HttpUrl.VIDEOINFO, j.toString(), callback);
    }


    /**
     * 获取评论列表
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void getCommentlist(String uid, String token, String page, String size, String showID, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("PageIndex", page);
        j.addProperty("PageSize", size);
        j.addProperty("ShowId", showID);
        post(HttpUrl.LIVECOMMENTLIST, j.toString(), callback);
    }

    /**
     * 点赞/取消
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void postLove(String uid, String token, String id, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("id", id);
        post(HttpUrl.GIVELIKE, j.toString(), callback);
    }

    /**
     * 评论
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void postVideoText(String uid, String token, String id, String Content, BernayCallback callback) {
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("id", id);
        j.addProperty("Content", Content);
        post(HttpUrl.LIVECOMMENT, j.toString(), callback);
    }
    /**
     * 获取七牛token
     *
     */
    public void getQiNiuToken(BernayCallback callback) {
        JsonObject j = new JsonObject();
        post(HttpUrl.GETTOKEN, j.toString(), callback);
    }

    /**
     * 上传头像
     * @param uid
     * @param token
     * @param headPicUrl
     * @param callback
     */
    public void saveUserHead(String uid,String token,String headPicUrl,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("HeadPicUrl", headPicUrl);
        post(HttpUrl.SAVE_USER_HRAD, j.toString(), callback);
    }

    /**
     * 修改昵称
     * @param uid
     * @param token
     * @param nickName
     * @param callback
     */
    public void saveUserNickName(String uid,String token,String nickName,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("NickName", nickName);
        post(HttpUrl.SAVE_USER_NICKNAME, j.toString(), callback);
    }

    /**
     * 修改真实名字
     * @param uid
     * @param token
     * @param realName
     * @param callback
     */
    public void saveUserRealName(String uid,String token,String realName,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("RealName", realName);
        post(HttpUrl.SAVE_USER_REALNAME, j.toString(), callback);
    }


    /**
     * 我的动态
     *
     * @param uid
     * @param token
     * @param PageIndex 页数
     * @param PageSize  列表数量
     * @param callback
     */
    public void getMyDynamic(String uid,String token,int PageIndex,int PageSize ,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("PageIndex", PageIndex);
        j.addProperty("PageSize", PageSize);
        post(HttpUrl.MY_DYNAMIC, j.toString(), callback);
    }

    /**
     * 我的相册
     *
     * @param uid
     * @param token
     * @param PageIndex
     * @param PageSize
     * @param callback
     */
    public void getMyAlbum(String uid,String token,int PageIndex,int PageSize ,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("PageIndex", PageIndex);
        j.addProperty("PageSize", PageSize);
        post(HttpUrl.MY_ALBUM, j.toString(), callback);
    }

    /**
     * 关于我们
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void getAboutUs(String uid,String token,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        post(HttpUrl.ABOUT_US, j.toString(), callback);
    }

    /**
     * 获取用户信息
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void getUserInfo(String uid,String token,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        post(HttpUrl.GET_USER_INFO, j.toString(), callback);
    }

    /**
     * 常见问题
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void getCommonQuestion(String uid,String token,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        post(HttpUrl.COMMON_QUESTION, j.toString(), callback);
    }

    /**
     * 问题详情
     *
     * @param uid
     * @param token
     * @param callback
     */
    public void getQuestionorAnsewr(String uid,String token,String Id,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("UserId", uid);
        j.addProperty("Token", token);
        j.addProperty("Id", Id);
        post(HttpUrl.QUESTIONORANSWER, j.toString(), callback);
    }
    /**
     * 获取设备列表
     * @param PageIndex
     * @param PageSize
     */
      public void getFixList(String PageIndex,String PageSize,BernayCallback callback){
          JsonObject j = new JsonObject();
          j.addProperty("PageIndex", PageIndex);
          j.addProperty("PageSize", PageSize);
          put(HttpUrl.GET_FIX_LIST,j.toString(),callback);
      }
//      /**
//     * 获取设备列表
//     * @param PageIndex
//     * @param PageSize
//     */
//      public void getFixList(String PageIndex,String PageSize,BernayCallback callback){
//          JsonObject j = new JsonObject();
//          j.addProperty("UserId", "50441");
//          j.addProperty("Token", "78A9-1645-E1E5-E276");
//          j.addProperty("PageIndex", PageIndex);
//          j.addProperty("PageSize", PageSize);
//          post(HttpUrl.GET_FIX_LIST,j.toString(),callback);
//      }
    /**
     * 获取设备状态
     * @param DeviceNumber
     */
    public void getFixStatus(String DeviceNumber,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("DeviceNumber", DeviceNumber);
        put(HttpUrl.GET_FIX_STATUS,j.toString(),callback);
    }
    /**
     * 添加设备
     * @param SnNumber //SN序列号
     * @param DeviceName //设备名称
     * @param CheckCode  //校验码
     */
    public void addFixing(String SnNumber,String DeviceName,String CheckCode,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("SnNumber", SnNumber);
        j.addProperty("DeviceName", DeviceName);
        j.addProperty("CheckCode", CheckCode);
        put(HttpUrl.ADD_FIXING,j.toString(),callback);
    }

//    /**
//     * 获取设备状态
//     * @param DeviceNumber
//     */
//    public void getFixStatus(String DeviceNumber,BernayCallback callback){
//        JsonObject j = new JsonObject();
//        j.addProperty("UserId", "50441");
//        j.addProperty("Token", "78A9-1645-E1E5-E276");
//        j.addProperty("DeviceNumber", DeviceNumber);
//        post(HttpUrl.GET_FIX_STATUS,j.toString(),callback);
//    }
    /**
     * 开启或关闭设备状态
     * @param DeviceNumber
     * @param Power
     */
    public void isOpenFixStatus(String DeviceNumber,String  Power,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("DeviceNumber", DeviceNumber);
        j.addProperty("Power", Power);
        put(HttpUrl.ISOPEN_FIXING_STATUS,j.toString(),callback);
    }

    /**
     * 消息列表
     * @param PageIndex
     * @param PageSize
     * @param Type  //1是系统消息 2是设备消息
     * @param callback
     */
    public void getUserMsg(int PageIndex,int PageSize,int Type,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("PageIndex", PageIndex);
        j.addProperty("PageSize", PageSize);
        j.addProperty("Type", Type);
        put(HttpUrl.GET_USER_MSG,j.toString(),callback);
    }

    /**
     * 设备消息详情列表
     * @param PageIndex
     * @param PageSize
     * @param Type 1:重力消息 2：电量消息 3：温度消息 4：呼叫对讲 5：设备通电
     * @param callback
     */
    public void deviceMsgList(int PageIndex,int PageSize,int Type,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("PageIndex", PageIndex);
        j.addProperty("PageSize", PageSize);
        j.addProperty("Type", Type);
        put(HttpUrl.DEVICE_MSG_LIST,j.toString(),callback);
    }

    /**
     * 系统消息和设备消息详情
     * @param MsgId
     * @param callback
     */
    public void systemDetail(String MsgId,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("MsgId", MsgId);
        put(HttpUrl.SYSTEM_DETAIL,j.toString(),callback);
    }

    /**
     * 消息删除
     * @param MsgId
     * @param callback
     */
    public void suerMsgDel(String MsgId,BernayCallback callback){
        JsonObject j = new JsonObject();
        j.addProperty("MsgId", MsgId);
        put(HttpUrl.USER_MSG_DEL,j.toString(),callback);
    }

    /**
     * post请求
     *
     * @param url      接口地址
     * @param json     json数据
     * @param callback 回调
     */
    private void post(String url, String json, BernayCallback callback) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            jsonObject.put("UserId", SharePreUtils.newInstance(BernayApp.mBernayApp).getUserId());

            jsonObject.put("Token", SharePreUtils.newInstance(BernayApp.mBernayApp).getToken());

            RequestBody body = FormBody.create(JSON, jsonObject.toString());
            Request.Builder builder = new Request.Builder();
            Request request = builder
                    .url(HttpUrl.IP + url)
                    .post(body)
                    .build();
            // LogUtils.json(HttpUrl.IP + url);
            mOkHttpClient.newCall(request).enqueue(callback);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /**
     * get请求
     *
     * @param url      接口地址
     * @param callback 回调
     */
    private void get(String url, BernayCallback callback) {

        Request.Builder builder = new Request.Builder();

        Request request = builder
                .url(HttpUrl.IP + url)
                .build();


        // LogUtils.e("\n接口地址：" + Constant.BASE_URL + url);
        mOkHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * put 请求
     *
     * @param url      接口地址
     * @param json     json数据
     * @param callback 回调
     */
    private void put(String url, String json, BernayCallback callback) {

        try {
            JSONObject jsonObject = new JSONObject(json);
            jsonObject.put("UserId", SharePreUtils.newInstance(BernayApp.mBernayApp).getUserId());

            jsonObject.put("Token", SharePreUtils.newInstance(BernayApp.mBernayApp).getToken());

            RequestBody body = FormBody.create(JSON, jsonObject.toString());
            Request.Builder builder = new Request.Builder();
            Request request = builder
                    .url(HttpUrl.IP + url)
                    .put(body)
                    .build();

            // LogUtils.e("\n接口地址：" + Constant.BASE_URL + url + "\n参数信息:\n");
            LogUtils.json(jsonObject.toString());
            mOkHttpClient.newCall(request).enqueue(callback);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
